package ws;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MySocketIOServer {

  static Logger logger = LoggerFactory.getLogger(MySocketIOServer.class);

  public static final String EVENT_ALARM_OCCUR = "ALARM_OCCUR";

  public static void main(String[] args) throws InterruptedException {

    Configuration config = new Configuration();
    config.setHostname("localhost");
    config.setPort(10003);
    config.setPingTimeout(20);
    SocketIOServer server = new SocketIOServer(config);
    server.addConnectListener(new ConnectListener() {// 添加客户端连接监听器
      @Override
      public void onConnect(SocketIOClient client) {
        logger.info(client.getRemoteAddress() + " web客户端接入");
        client.sendEvent("helloPush", "hello");
      }
    });

//    // 握手请求
//    server.addEventListener("helloevent", HelloUid.class, new DataListener<HelloUid>() {
//      @Override
//      public void onData(final SocketIOClient client, HelloUid data, AckRequest ackRequest) {
//        // 握手
//        if (data.getMessage().equals("hello")) {
//          int userid = data.getUid();
//          logger.info(Thread.currentThread().getName() + "web读取到的userid：" + userid);
//
//
//          // send message back to client with ack callback
//          // WITH data
//          client.sendEvent("hellopush", new AckCallback<String>(String.class) {
//            @Override
//            public void onSuccess(String result) {
//              logger.info("ack from client: " + client.getSessionId() + " data: " + result);
//            }
//          }, sessionTime);
//
//        } else {
//          logger.info("行情接收到了不应该有的web客户端请求1111...");
//        }
//      }
//    });

    server.addEventListener(EVENT_ALARM_OCCUR, HelloMsg.class,
            new DataListener<HelloMsg>() {
              @Override
              public void onData(SocketIOClient client, HelloMsg data,
                                 AckRequest ackSender) throws Exception {
                logger.warn("client data:" + data);
                server.getBroadcastOperations().sendEvent(EVENT_ALARM_OCCUR, "hi");
              }
            });

    server.start();

    new Thread(new Runnable() {
      @Override
      public void run() {

        while (true) {
          try {

            Gson gson = new GsonBuilder().create();
            Map<String, String> maps = new HashMap<String, String>();
            maps.put("message_type", "place_order" + new Date());

            server.getBroadcastOperations().sendEvent(EVENT_ALARM_OCCUR, gson.toJson(maps));

          } catch (Exception e) {
            e.printStackTrace();
          }
          try {
            Thread.sleep(3000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

      }
    }).start();

    Thread.sleep(Integer.MAX_VALUE);

    server.stop();
  }


}
